function DynamicWallMaterialProperty(options) {
    this._definitionChanged = new Cesium.Event()
    this._color = undefined
    this._colorSubscription = undefined
    this.color = options.color
    this.duration = options.duration || 1000
    this.trailImage = options.trailImage
    this.lineImage = options.lineImage
    this._time = new Date().getTime()
}

/**
 * 带方向的墙体
 * @param {*} options.get:true/false
 * @param {*} options.count:数量
 * @param {*} options.freely:vertical/standard
 * @param {*} options.direction:+/-
 */
function _getDirectionWallShader(options) {
    if (options && options.get) {
        var materail = 'czm_material czm_getMaterial(czm_materialInput materialInput)\n\
                                {\n\
                                    czm_material material = czm_getDefaultMaterial(materialInput);\n\
                                    vec2 st = materialInput.st;'
        materail += 'vec4 colorImage = texture2D(lineImage, vec2(fract(st.s), fract(float(' +
            options.count +
            ')*st.t' +
            options.direction +
            ' time)));\n\ '

        // 泛光
        materail += '\n\
                                    vec4 colorBack = texture2D(trailImage, st);\n\
                                    vec4 fragColor;\n\
                                    fragColor.rgb = (colorImage.rgb+colorBack.rgb+color.rgb) / 3.0;\n\
                                    fragColor = czm_gammaCorrect(fragColor);\n\
                                    material.diffuse = colorBack.rgb;\n\
                                    material.alpha = colorBack.a;\n\
                                    material.emission = fragColor.rgb;\n\
                                    return material;\n\
                                }'
        return materail
    }
}

Object.defineProperties(DynamicWallMaterialProperty.prototype, {
    isConstant: {
        get: function() {
            return false
        }
    },
    definitionChanged: {
        get: function() {
            return this._definitionChanged
        }
    },
    color: Cesium.createPropertyDescriptor('color')
})

var MaterialType = 'wallType' + parseInt(Math.random() * 1000)
DynamicWallMaterialProperty.prototype.getType = function(time) {
    return MaterialType
}

DynamicWallMaterialProperty.prototype.getValue = function(time, result) {
    if (!Cesium.defined(result)) {
        result = {}
    }
    result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color)
    result.trailImage = this.trailImage
    result.lineImage = this.lineImage
    if (this.duration) {
        result.time = ((new Date().getTime() - this._time) % this.duration) / this.duration
    }
    return result
}

DynamicWallMaterialProperty.prototype.equals = function(other) {
    return (
        this === other ||
        (other instanceof DynamicWallMaterialProperty && Cesium.Property.equals(this._color, other._color))
    )
}

Cesium.Material._materialCache.addMaterial(MaterialType, {
    fabric: {
        type: MaterialType,
        uniforms: {
            color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
            trailImage: Cesium.Material.DefaultImageId,
            lineImage: Cesium.Material.DefaultImageId,
            time: -20
        },
        source: _getDirectionWallShader({
            get: true,
            count: 2.0,
            freely: 'vertical',
            direction: '-'
        })
    },
    translucent: function(material) {
        return true
    }
})
Cesium.DynamicWallMaterialProperty = DynamicWallMaterialProperty
